package com.jhgsys.internal.hazard.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.hazard.entity.HazardReportAuditRecord;
import com.jhgsys.internal.hazard.service.IHazardReportAuditRecordService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * 隐患报告审核记录 Controller
 *
 * @author wanghw
 * @date 2024-08-20 09:23:12
 */
@Slf4j
@Validated
@Controller
public class HazardReportAuditRecordController extends BaseController {

    @Autowired
    private IHazardReportAuditRecordService hazardReportAuditRecordService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardReportAuditRecord")
    @RequiresPermissions("hazardReportAuditRecord:view")
    public String hazardReportAuditRecordIndex(){
        return FebsUtil.view("hazard/hazardReportAuditRecord/hazardReportAuditRecordList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardReportAuditRecord/add")
    @RequiresPermissions("hazardReportAuditRecord:add")
    public String hazardReportAuditRecordAdd(){
        return FebsUtil.view("hazard/hazardReportAuditRecord/hazardReportAuditRecordEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardReportAuditRecord/update/{id}")
    @RequiresPermissions("hazardReportAuditRecord:edit")
    public String hazardReportAuditRecordUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("hazardReportAuditRecord", hazardReportAuditRecordService.findHazardReportAuditRecordById(id));
        return FebsUtil.view("hazard/hazardReportAuditRecord/hazardReportAuditRecordEdit");
    }

    /**
     * 查询（所有）
     * @author wanghw
     * @date 2024-08-20 09:23:12
     */
    @GetMapping("hazardReportAuditRecord")
    @ResponseBody
    @RequiresPermissions("hazardReportAuditRecord:view")
    public FebsResponse getAllHazardReportAuditRecords(HazardReportAuditRecord hazardReportAuditRecord) {
        return new FebsResponse().success().data(hazardReportAuditRecordService.findHazardReportAuditRecordList(hazardReportAuditRecord));
    }

    /**
     * 查询（分页）
     * @author wanghw
     * @date 2024-08-20 09:23:12
     */
    @GetMapping("hazardReportAuditRecord/list")
    @ResponseBody
    @RequiresPermissions("hazardReportAuditRecord:view")
    public FebsResponse hazardReportAuditRecordList(QueryRequest request, HazardReportAuditRecord hazardReportAuditRecord) {
        Map<String, Object> dataTable = getDataTable(this.hazardReportAuditRecordService.findHazardReportAuditRecordPage(request, hazardReportAuditRecord));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 HazardReportAuditRecord
     * @author wanghw
     * @date 2024-08-20 09:23:12
     */
    @Log("新增HazardReportAuditRecord")
    @PostMapping("hazardReportAuditRecord")
    @ResponseBody
    @RequiresPermissions("hazardReportAuditRecord:add")
    public FebsResponse addHazardReportAuditRecord(@Valid HazardReportAuditRecord hazardReportAuditRecord) throws FebsException {
        try {
            hazardReportAuditRecord.setCreatedBy(getCurrentUser().getUserName());
            this.hazardReportAuditRecordService.createHazardReportAuditRecord(hazardReportAuditRecord);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增HazardReportAuditRecord失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 HazardReportAuditRecord
     * @author wanghw
     * @date 2024-08-20 09:23:12
     */
    @Log("逻辑删除HazardReportAuditRecord")
    @GetMapping("hazardReportAuditRecord/delete/{id}")
    @ResponseBody
    @RequiresPermissions("hazardReportAuditRecord:delete")
    public FebsResponse deleteHazardReportAuditRecord(@PathVariable("id") String hazardReportAuditRecordIds) throws FebsException {
        try {
            HazardReportAuditRecord hazardReportAuditRecord = new HazardReportAuditRecord();
            hazardReportAuditRecord.setDeletedBy(getCurrentUser().getUserName());
            hazardReportAuditRecord.setDeletedTime(new Date());
            hazardReportAuditRecord.setDeletedFlg(FebsConstant.INVALID);
            this.hazardReportAuditRecordService.deleteHazardReportAuditRecord(hazardReportAuditRecord,hazardReportAuditRecordIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardReportAuditRecord失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
    * 物理删除 HazardReportAuditRecord
    * @author wanghw
    * @date 2024-08-20 09:23:12
    */
    @Log("物理删除HazardReportAuditRecord")
    @GetMapping("hazardReportAuditRecord/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("hazardReportAuditRecord:delete")
    public FebsResponse physicallyDeleteHazardReportAuditRecord(@PathVariable("id") String hazardReportAuditRecordIds) throws FebsException {
        try {
            this.hazardReportAuditRecordService.physicallyDeleteHazardReportAuditRecord(hazardReportAuditRecordIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardReportAuditRecord失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 修改 HazardReportAuditRecord
     * @author wanghw
     * @date 2024-08-20 09:23:12
     */
    @Log("修改HazardReportAuditRecord")
    @PostMapping("hazardReportAuditRecord/update")
    @ResponseBody
    @RequiresPermissions("hazardReportAuditRecord:edit")
    public FebsResponse updateHazardReportAuditRecord(HazardReportAuditRecord hazardReportAuditRecord) throws FebsException {
        try {
            hazardReportAuditRecord.setUpdatedBy(getCurrentUser().getUserName());
            this.hazardReportAuditRecordService.updateHazardReportAuditRecord(hazardReportAuditRecord);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改HazardReportAuditRecord失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 导出 HazardReportAuditRecord的excel
     * @author wanghw
     * @date 2024-08-20 09:23:12
     */
    @PostMapping("hazardReportAuditRecord/excel")
    @ResponseBody
    @RequiresPermissions("hazardReportAuditRecord:view")
    public void export(QueryRequest queryRequest, HazardReportAuditRecord hazardReportAuditRecord, HttpServletResponse response) throws FebsException {
        try {
            List<HazardReportAuditRecord> hazardReportAuditRecords = this.hazardReportAuditRecordService.findHazardReportAuditRecordPage(queryRequest, hazardReportAuditRecord).getRecords();
            ExcelKit.$Export(HazardReportAuditRecord.class, response).downXlsx(hazardReportAuditRecords, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
